001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb.builder.utils; 021 022 import javax.swing.*; 023 024 /** 025 * Implements a parser for returning {@link javax.swing.KeyStroke KeyStroke} 026 * from a string representation of the key stroke. 027 * <p>Java already provides such a facility but, unfortunately, the parsing 028 * uses non-standard names for meta key names.</p> 029 * <p>This class provides a parser that combines both parsers into one: 030 * the passed key stroke string can be either in Java's parser format, or the 031 * more standard "Ctrl+C" format.</p> 032 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 033 */ 034 public class KeyStrokeParser { 035 /** 036 * Returns <code>null</code> if the key stroke string could not be parsed. 037 */ 038 public static KeyStroke getKeyStroke(final String keyStrokeString) { 039 KeyStroke stroke = KeyStroke.getKeyStroke(keyStrokeString); 040 if (stroke != null) { 041 return stroke; 042 } 043 044 String upStroke = keyStrokeString.toUpperCase(); 045 stroke = KeyStroke.getKeyStroke(upStroke); 046 if (stroke != null) { 047 return stroke; 048 } 049 050 final StringBuffer newKeyStroke = new StringBuffer(); 051 final String code = upStroke.substring(upStroke.lastIndexOf("+") + 1); 052 if (upStroke.indexOf("CTRL") != -1) { 053 newKeyStroke.append("control "); 054 } 055 056 if (upStroke.indexOf("SHIFT") != -1) { 057 newKeyStroke.append("shift "); 058 } 059 060 if (upStroke.indexOf("ALT") != -1) { 061 newKeyStroke.append("alt "); 062 } 063 064 if (upStroke.indexOf("META") != -1) { 065 newKeyStroke.append(" meta"); 066 } 067 068 newKeyStroke.append(code); 069 070 return KeyStroke.getKeyStroke(newKeyStroke.toString()); 071 } 072 }